package com.ihome.android.academy.service;

import android.app.Service;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.ihome.android.common.ConfigUtil;
import com.ihome.android.http.HttpKeys;
import com.ihome.android.market2.aidl.GetMarketInfoAidl;
import com.ihome.android.market2.asynctask.AppUpdateTask;
import com.ihome.android.market2.info.CertificateInfo;
import com.ihome.android.market2.info.GetMarketInfoReqInfo;
import com.ihome.android.mod.LoginMod;
import com.ihome.android.util.Confirmation;
import com.ihome.android.util.Util;
import com.ihome.android.util.log.LogUtil;

public class GetMarketInfoService extends Service {
	private final String	TAG	= GetMarketInfoService.class.getSimpleName();
	
	private String			oprateResult	= "";

	@Override
	public IBinder onBind(Intent intent) {
		LogUtil.i(TAG, "GetInfo-->onBind()----");
		return binder;
	}

	GetMarketInfoAidl.Stub	binder	= new GetMarketInfoAidl.Stub() {

										@Override
										public String getInfo(String reqParam) throws RemoteException {
											return serviceGetInfo(reqParam);
										}

									};

	private String serviceGetInfo(String reqParam) {
		LogUtil.v(TAG, "GetInfo reqParam-->" + reqParam);
		String result = "";
		if (!TextUtils.isEmpty(reqParam)) {
			GetMarketInfoReqInfo info = new Gson().fromJson(reqParam, GetMarketInfoReqInfo.class);
			String pkgName = Confirmation.getProcessNameByPID(GetMarketInfoService.this, Binder.getCallingPid());
			info.setPackageName(pkgName);
			LogUtil.v(TAG, "GetInfo callApp PkgName --> " + pkgName);
			if (pkgName != null && info != null ) {
				PackageInfo pkinfo = Confirmation.getPackageInfoByProcessName(GetMarketInfoService.this, pkgName);
				CertificateInfo certificate = Confirmation.checkAPKSignature(GetMarketInfoService.this, pkinfo);
				if ( certificate != null && certificate.islegal() || ConfigUtil.isDebug) {
					//checkAppUpdate();
					result = getMarketInfo(info, pkgName);
				}
				else {
					LogUtil.e(TAG, "GetInfo APP : " + pkgName + "  ---> 应用合法性校验失败 ");
				}
			}
		}
		return result;
	};
	
	private String getMarketInfo(GetMarketInfoReqInfo info, String callAppPkgName) {

		String sessionkey_before = ConfigUtil.SessionKey;
		
		LogUtil.d(TAG, " --> getMarketInfo sessionkey_before = " + sessionkey_before);
		LogUtil.d(TAG, " --> getMarketInfo GetMarketInfoReqInfo = " + info.toString());
		synchronized (GetMarketInfoSynObj.getInstance()) {
			try {
				
				if((info.getOperate() == 0 && TextUtils.isEmpty(ConfigUtil.SessionKey)) || info.getOperate()== 1){
					if (!TextUtils.isEmpty(Util.getOTTUserName(this.getApplicationContext()))) {
						LoginMod.autoLogin(this);
					}
					else {
						LoginMod.autoRegister(this, "", false);
					}
					
					LogUtil.d(TAG, " --> getMarketInfo = " + " GetInfo has lost object lock");
					GetMarketInfoSynObj.getInstance().wait();
					LogUtil.d(TAG, " --> getMarketInfo = " + " GetInfo has get object lock");
				}
				

			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		
		boolean getSessionKeySuccess = false;
		if((info.getOperate() == 0 && !TextUtils.isEmpty(ConfigUtil.SessionKey)) || 
				(info.getOperate()== 1 && !sessionkey_before.equals(ConfigUtil.SessionKey))){
			getSessionKeySuccess = true;
		}
		
		if(getSessionKeySuccess)
			oprateResult = "{code:" + 0 + ", description:" + "SessionKey获取成功" + ", sessionKey:" + ConfigUtil.SessionKey +"}";
		else
			oprateResult = "{code:" + -1 + ", description:" + "SessionKey获取失败" + ", sessionKey:\"\"}";
		return oprateResult;
	}
	
	private void checkAppUpdate(){
		Bundle b = new Bundle();
		b.putString(HttpKeys.KEY_PACKAGENAME, ConfigUtil.MINSHENG_KEY);
		new AppUpdateTask(getApplicationContext()).execute(b);
	}
	
}
